package at.com.vn.chess.service;

import java.util.ArrayList;

import at.com.vn.chess.bean.PrivateChatRoomBean;
import at.com.vn.chess.entity.ChatUser;
import vn.com.bip.office.smart.util.XMLHelper;

public class ChatPrivateService extends BaseService {

	public ChatPrivateService(String inputStrXml) {
		super(inputStrXml);
		logger.info( "Bat dau" );
	}

	@SuppressWarnings( "unchecked" )
	@Override
	public void doService() {

		// Create initation xml error message
		String errMessage = "Some error";
		outputStrXml = XMLHelper.convertMessageToXML("ChatPrivate", "ERR_MSG",
				"MSG", errMessage);

		if (inputStrXml.trim().equals("")) {
			logger.error( "Client truyen len mot file inputStrXml rong" );
			return;
		}

		// get user
		ArrayList<ChatUser> receiverLstUser = (ArrayList<ChatUser>) XMLHelper
				.convertXMLToListObject(inputStrXml, ChatUser.class);
		ChatUser currentUser = receiverLstUser.get(0);

		// do join private chat room
		// if not available chatRoom, create new chatRoom
		PrivateChatRoomBean currentChatRoom = getChatRoomByID(currentUser
				.getPrivateRoomID());
		if (currentChatRoom == null) {
			PrivateChatRoomBean newChatRoom = new PrivateChatRoomBean();
			newChatRoom.isAlive = 1;
			newChatRoom.lstChatUser.add(currentUser);
			newChatRoom.privateRoomID = currentUser.getPrivateRoomID();
			privateChatRoomBeanList.add(newChatRoom);
			currentChatRoom = newChatRoom;
		} else {
			if (!isAvailable(currentUser, currentChatRoom.lstChatUser)) {
				currentChatRoom.lstChatUser.add(currentUser);
			} else {
				remove(currentUser, currentChatRoom.lstChatUser);
				currentChatRoom.lstChatUser.add(currentUser);
			}
		}

		// do chat, left private chat room(= copy and forward
		// currentChatRoom.lstChatUser to all client)
		// send data
		outputStrXml = getMXLResult(inputStrXml, currentUser,
				currentChatRoom.lstChatUser);

	}

	private void remove(ChatUser user, ArrayList<ChatUser> lstUser) {
		for (ChatUser iuser : lstUser) {
			if (iuser.getUserID().equals(user.getUserID())) {
				lstUser.remove(iuser);
				break;
			}
		}
	}

	private boolean isAvailable(ChatUser user, ArrayList<ChatUser> lstUser) {
		boolean isAvail = false;
		for (ChatUser iuser : lstUser) {
			if (iuser.getUserID().equals(user.getUserID())) {
				isAvail = true;
				break;
			}
		}
		return isAvail;
	}

	public PrivateChatRoomBean getChatRoomByID(int privateRoomID) {
		for (PrivateChatRoomBean chatRoom : privateChatRoomBeanList) {
			if (chatRoom.privateRoomID == privateRoomID) {
				return chatRoom;
			}
		}
		return null;
	}

	private String getMXLResult(String xmlInfo, ChatUser activeUser,
			ArrayList<ChatUser> lstUser) {

		String processName = XMLHelper.getProcessName(xmlInfo);

		// Reset other user's status
		for (ChatUser otherUser : lstUser) {
			if (!activeUser.getUserID().equals(otherUser.getUserID())) {
				// Do not show chat message for other user
				otherUser.setIsActive(0);
			} else {
				// when left
				if (processName.contains("_left")) {
					// not in chess table
					otherUser.setIsDeleted(1);
				}
			}

		}

		// Broadcast it to all listening clients
		return XMLHelper.convertListObjectToXML(lstUser, processName,
				ChatUser.class);
	}

}